Spring Cloud开发实践(七): 集成Consul配置中心 |
您所在的位置:网站首页 › spring cloud 配置中心 › Spring Cloud开发实践(七): 集成Consul配置中心 |
目录
Spring Cloud开发实践(一): 简介和根模块
Spring Cloud开发实践(二): Eureka服务和接口定义
Spring Cloud开发实践(三): 接口实现和下游调用
Spring Cloud开发实践(四): Docker部署
Spring Cloud开发实践(五): Consul - 服务注册的另一个选择
Spring Cloud开发实践(六): 基于Consul和Spring Cloud 2021.0的演示项目
Spring Cloud开发实践(七): 集成Consul配置中心
Spring Cloud Consul Config
Consul 通过 Key/Value 功能集中管理存储配置信息, 通过 Spring Cloud Consul Config 可以实现 Config Server 和 Client 的关联. 在 Spring 启动的 bootstrap 阶段, 配置会被载入环境上下文. 配置前缀, 路径和优先级默认情况下, 配置的路径前缀是 /config , 不同的 application 和 profile 对应不同的配置路径, 例如对应应用 "testApp" 和 "dev" profile 的配置, 会涉及以下路径 config/testApp,dev/ config/testApp/ config/application,dev/ config/application/这个列表从上往下分别对应的配置优先级从高到低, 优先级高的同样配置项会覆盖优先级低的配置项. config/application/ 全局公共配置, 对应使用 config 前缀的所有应用 config/application,dev/ 全局dev公共配置, 对应使用 config 前缀的所有, 且启用 dev profile 的应用 config/testApp/ 对应使用 config 前缀的, 名称为 testApp 的应用 config/testApp,dev/ 对应使用 config 前缀的, 名称为 testApp, 且启用 dev profile 的应用注意: 配置对应的 profile 和节点应用名是平级的, config/service-name,dev/data 这样, data 是配置的子项, 不要把 profile 加到 data 去了 在项目中启用 Consul Config如果要使用 Consul 的分布式配置(Distributed Configuration), 需要添加 spring-cloud-starter-consul-config 的依赖 spring-cloud-starter-consul-discovery 不带 spring-cloud-starter-consul-config, 如果需要用 Consul Config, 需要单独添加依赖 org.springframework.cloud spring-cloud-starter-consul-config也可以直接用 spring-cloud-starter-consul-all, 包含了 spring-cloud-starter-consul-discovery 和 spring-cloud-starter-consul-config org.springframework.cloud spring-cloud-starter-consul-all 配置文件 application.yml添加了 consul-config 依赖之后, 在 application.yml 就要增加对应的设置 spring.config.import = consul: 否则启动会报错, Spring Boot 在 2.4 版本之后新增了这个项(spring.config.import property)用于导入配置, 并且是默认的配置方式. # properties spring.config.import=optional:consul: # yaml spring: config: import: 'consul:'上面的配置, 如果启动时import失败会导致启动失败, 如果不强制 import, 可以加上 optional: # properties spring.config.import=optional:consul: # yaml spring: config: import: 'optional:consul:'上面的这两个配置, 都会用默认的地址 http://localhost:8500 去请求 Consul 服务, 如果需要自定义地址, 可以通过配置spring.cloud.consul.host和spring.cloud.consul.port, spring: cloud: consul: host: 10.123.123.123 port: 8501或者使用 spring.config.import=optional:consul:myhost:8500对应以上配置, 在 Spring 启动的 bootstrap 阶段会通过 Consul 去获取 key = config/dummy-service/data 对应的 value (假定这个模块的 application.name = dummy-service), 对应value格式为 yaml, 需要增加配置 # yaml spring: cloud: consul: config: format: YAML prefix: config data-key: data其中 format: YAML 设置配置格式 prefix: config修改 config/dummy-service/data 的前缀 data-key: data修改 config/dummy-service/data 的后缀默认的请求路径生成基于 spring.cloud.consul.config.name , 值默认等于 spring.application.name spring.cloud.consul.config.default-context , 这个值默认等于 application spring.profiles.active , 可以在启动时通过 VM Option -Dspring.profiles.active=xxx 指定如果不想用默认的, 想自己指定, 可以用如下的方式 # properties spring.config.import=optional:consul:myhost:8500/config/custom/context/one;/config/custom/context/two上面的设置将只从这两个Key/Value路径读取配置, 注意路径的对应关系, 在import中体现前缀 config, 但是不体现后缀 data /config/custom/context/one/data /config/custom/context/two/data 配置自动更新, Config WatchConsul Config Watch 使用 consul 的路径前缀对配置更新进行检查, 当配置变化时会产生一个 Refresh Event, 等价于请求 /refresh actuator endpoint. 默认的检查频率为 1000 单位毫秒, 可以通过 spring.cloud.consul.config.watch.delay 配置 如果要禁用配置自动更新, 需要设置 spring.cloud.consul.config.watch.enabled=false Consul 配置管理 通过 WEB 界面默认为 http://127.0.0.1:8500 可以在 Key/Value 中直接添加, 记得格式要改为 YAML 通过命令行读取 $ ./consul kv get foo bar $ ./consul kv get config/application/data cassandra: host: 127.0.0.1:9042,127.0.0.2:9042 user: my_user password: my_pass使用文件data.yml中的内容, 直接写入 $ ./consul kv put config/application/data @data.yml Success! Data written to: config/application/data The data can be retrieved the same way, 使用配置经过以上配置, 在项目中就可以通过 @Configuration 获取 Consul 中配置的信息 @Slf4j @Configuration public class CommonConfig { @Value("${common.name}") private String name = "Dummy"; @Value("${common.code}") private String code = "001"; public String getName() {return name;} public void setName(String name) {this.name = name;} public String getCode() {return code;} public void setCode(String code) {this.code = code;} @PostConstruct public void postConstruct() { log.info("name: {}", name); log.info("code: {}", code); } } 参考 Spring Docs: Distributed Configuration with Consul 总结以上是真正的电脑专家为你收集整理的Spring Cloud开发实践(七): 集成Consul配置中心的全部内容,希望文章能够帮你解决所遇到的问题。 如果觉得真正的电脑专家网站内容还不错,欢迎将真正的电脑专家推荐给好友。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |